//
//  DMApiEncryptor.m
//  DMHttpManager
//
//  Created by zgy on 2025/6/30.
//

#import "DMApiEncryptor.h"
#import "NSString+JKHash.h"
#import "NSString+JKEncrypt.h"


@interface DMApiEncryptor ()


@property (nonatomic,   copy) NSString *token;
@property (nonatomic,   copy) NSString *appID;

@property (nonatomic, strong) NSString *safeKey;


@end


@implementation DMApiEncryptor

- (instancetype)initWithToken:(NSString *)token appID:(NSString *)appID
{
    self = [super init];
    if (self) {
        _token = token;
        _appID = appID;
    }
    return self;
}

- (void)resetToken:(NSString *)token appID:(NSString *)appID
{
    _token = token;
    _appID = appID;
    _safeKey = nil;
}


#pragma mark - Hashing Methods
- (NSString *)encrypt3DES:(NSString *)string
{
    return [string jk_encryptedWith3DESUsingKey:self.safeKey andIV:nil];
}
- (NSString *)decrypt3DES:(NSString *)string
{
    return [string jk_decryptedWith3DESUsingKey:self.safeKey andIV:nil];
}


#pragma mark- lazy
- (NSString *)safeKey
{
    if (_safeKey == nil) {
        NSString *tmp = [NSString stringWithFormat:@"%@%@", self.token, self.appID];
        _safeKey = tmp.jk_md5String;
    }
    
    return _safeKey;
}




@end
